//
//  OrderLogic.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/2/3.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "OrderLogic.h"
#import "OrderModel.h"
@implementation OrderLogic

- (instancetype)init
{
    self = [super init];
    if (self) {
        _page = 1;
        _dataArray = [NSMutableArray array];
    }
    return self;
}

-(void)loadData
{
    if (_page == 1) {
        [_dataArray removeAllObjects];
    }
    
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"rank_api" A:@"charm_rank_list"];
    
    if([self.type isEqualToString:@"1"])
    {
        //魅力.... todo
    }
    else if([self.type isEqualToString:@"2"])
    {
        url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"rank_api" A:@"wealth_rank_list"];
    }
    else if([self.type isEqualToString:@"3"])
    {
        url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"page_data_api" A:@"get_news_user_list"];
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(_page) forKey:@"page"];
    [param setObject:self.subClass forKey:@"type"];
    
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {

    } success:^(id responseObject) {
        if (_page == 1) {
            [_dataArray removeAllObjects];
        }
        
        //取出我的排名
        _myRankings = [NSString stringWithFormat:ASLocalizedString(@"我的排名:%@"),[responseObject valueForKey:@"order_num"]];
        NSArray *tmpArr = [NSMutableArray modelArrayWithClass:[OrderModel class] json:[responseObject valueForKey:@"list"]];
        NSMutableArray *list = [NSMutableArray arrayWithArray:tmpArr];
        if(_page == 1 && ![self.type isEqualToString:@"3"])//如果是第一页，并且不是最新，把第一个取出来
        {
            if(list.count !=0)
            {
                _firstUser = [list firstObject];
                [list removeObjectAtIndex:0];
            }
            else
            {
                _firstUser = [[OrderModel alloc] init];
            }
        }
        else if(_page == 1 && [self.type isEqualToString:@"3"])//如果是最新的话接口没有返回charm 字段，这里就特殊处理一下
        {
            NSArray *tmpArr = [NSMutableArray modelArrayWithClass:[OrderModel class] json:[responseObject valueForKey:@"data"]];
            list = [NSMutableArray arrayWithArray:tmpArr];
        }
        
        [_dataArray addObjectsFromArray:list];
        
        if (self.delegagte && [self.delegagte respondsToSelector:@selector(requestDataCompleted)]) {
            [self.delegagte requestDataCompleted];
        }
    } failure:^(NSString *error) {
//        [MBProgressHUD showTipMessageInWindow:error];
//        if (self.delegagte && [self.delegagte respondsToSelector:@selector(requestDataCompleted)]) {
//            [self.delegagte requestDataCompleted];
//        }
    } hasCache:YES];
}

@end
